#ifndef SESSIONFRIENDAREA_H
#define SESSIONFRIENDAREA_H

#pragma once
#include <QObject>
#include <QScrollArea>
#include<QScrollBar>
#include<QVBoxLayout>
#include<QPushButton>
#include<QMouseEvent>
#include<QLabel>
#include<QPainter>

enum ItemType {
    SessionItemType,
    FriendItemType,
    ApplyItemType
};


/////////////////////////////
///消息会话列表区
/////////////////////////////
class SessionFriendArea : public QScrollArea
{
    Q_OBJECT
public:
    SessionFriendArea();

    //清空所有元素
    void clear();
    // 添加⼀个元素, text 可能表⽰签名, 也可能表⽰最后⼀条消息.
    void addItem(ItemType itemType, const QString& id, const QIcon& avatar,const QString& name, const QString& text);
    // 点击指定下标的元素
    void clickItem(int index);

    bool eventFilter(QObject* watched, QEvent* event)override;
private:
    QWidget* container;
};

class SessionFriendItem : public QWidget{
    Q_OBJECT
public:
    SessionFriendItem(QWidget* owner, const QIcon& avatar, const QString&name, const QString &lastMessage);

    void mousePressEvent(QMouseEvent* event)override;
    // 选中这⼀项要做的事情. 单独提出⼀个函数, 为了后续能直接调⽤.
    void select();

    void enterEvent(QEnterEvent* event) override;
    void leaveEvent(QEvent* event) override;
    void paintEvent(QPaintEvent *event) override;

    // 如果该元素被点击时, 要做哪些事情
    virtual void active() { }

protected:
    QGridLayout* layout;
    // 显⽰最后⼀条消息/描述信息
    QLabel* messageLabel;
    // 是否被选中
    bool selected;
    // 该 Item 所属的 QWidget. 此处填写 SessionFriendArea
    QWidget* owner;

};

//会话列表元素
class SessionItem : public SessionFriendItem {
    Q_OBJECT
private:
    QString chatSessionId;
public:
    SessionItem(QWidget* owner, const QString& chatSessionId, const QIcon&avatar, const QString &name, const QString &lastMessage);
    void active() override;

    //更新最后一条消息
    void updateLastMessage(const QString& chatSessionId);
};

//好友列表元素
class FriendItem : public SessionFriendItem {
    Q_OBJECT
private:
    QString userId;
public:
    FriendItem(QWidget* owner, const QString& friendId, const QIcon& avatar,const QString &name, const QString &description);
    void active() override;
};

//好友申请列表元素
class ApplyItem : public SessionFriendItem {
    Q_OBJECT
private:
    // 申请⼈的⽤⼾ id
    QString userId;
public:
    ApplyItem(QWidget* owner, const QString& userId, const QIcon& avatar, const QString& name);
    void active() override;

private:
    //同意好友申请
    void acceptFriend();
    void acceptFriendDone(const QString& userId, const QString& reason);

    //拒绝好友申请
    void rejectFriend();
    void rejectFriendDone(const QString& userId, const QString& reason);
};




#endif // SESSIONFRIENDAREA_H
